package priv.lirenhe.dissertation.cacher;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CacherManager {
	
	private static Map<String, Cacher> caches = new ConcurrentHashMap<String, Cacher>();
	  /**
	   * 存入缓存
	   * @param key
	   * @param cache
	   */
	  public void putCache(String key, Cacher cacher) {
		  synchronized (CacherManager.caches) {
			  caches.put(key, cacher);
		  }
	  }
	  /*
	  synchronized (CacherManager.caches) {
		  
	  }
	  */
	  /**
	   * 存入缓存
	   * @param key
	   * @param cache
	   */
	  public void putCache(String key, Object datas, long timeOut) {
		  synchronized (CacherManager.caches) {
			  timeOut = timeOut > 0 ? timeOut : 0L;
			  putCache(key, new Cacher(datas, timeOut, System.currentTimeMillis()));
		  }
	  }
	  
	  /**
	   * 获取对应缓存
	   * @param key
	   * @return
	   */
	  public Cacher getCacherByKey(String key) {
		  synchronized (CacherManager.caches) {
			  if (this.isContains(key)) {
				  return caches.get(key);
			  }
		  }
	    return null;
	  }
	  
	  /**
	   * 获取对应缓存
	   * @param key
	   * @return
	   */
	  public Object getCacheDataByKey(String key) {
		  synchronized (CacherManager.caches) {
			  if (this.isContains(key)) {
				  return caches.get(key).getDatas();
			  }
		  }
	    return null;
	  }

	  /**
	   * 获取所有缓存
	   * @param key
	   * @return
	   */
	  public Map<String, Cacher> getCacheAll() {
		  Map<String, Cacher> cachess = new ConcurrentHashMap<String,Cacher>();
		  synchronized (CacherManager.caches) {
			  cachess.putAll(caches);
			  caches.clear();
			  return cachess;
		  }
	  }

	  /**
	   * 判断是否在缓存中
	   * @param key
	   * @return
	   */
	  public boolean isContains(String key) {
		  synchronized (CacherManager.caches) {
			  return caches.containsKey(key);
		  }
	  }

	  /**
	   * 清除所有缓存
	   */
	  public void clearAll() {
		  synchronized (CacherManager.caches) {
			  caches.clear();
		  }
	  }

	  /**
	   * 清除对应缓存
	   * @param key
	   */
	  public void clearByKey(String key) {
		  synchronized (CacherManager.caches) {
			  if (this.isContains(key)) {
				  caches.remove(key);
			  }
		  }
	  }

	  /**
	   * 缓存是否超时失效
	   * @param key
	   * @return
	   */
	  public boolean isTimeOut(String key) {
	    if (!caches.containsKey(key)) {
	      return true;
	    }
	    Cacher cacher = caches.get(key);
	    long timeOut = cacher.getTimeOut();
	    long lastRefreshTime = cacher.getLastRefeshTime();
	    if (timeOut != 0 && System.currentTimeMillis() - lastRefreshTime >= timeOut) {
	    	return true;
	    }
	    return false;
	  }

	  /**
	   * 获取所有key
	   * @return
	   */
	  public Set<String> getAllKeys() {
		  synchronized (CacherManager.caches) {
			  return caches.keySet();
		  }
	  }
	  
	  public boolean isEmpty(){
		  synchronized (CacherManager.caches) {
			  return caches.isEmpty();
		  }
	  }
	  
	  public int getCacherSize(){
		  return caches.size();
	  }
	  
}
